using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;

namespace ClassifierLib.Classifier
{
    public class AsyncronousLCS : LCS
    {
        private Thread _workerThread;

        /// <summary>
        /// Default constructor, creates standard filters
        /// </summary>
        public AsyncronousLCS()
            : base()
        {
            _workerThread = new Thread(new ParameterizedThreadStart(TrainWorker));
        }

        public override void Train(TrainingSet TrainingSet)
        {
            if (_workerThread.IsAlive)
                throw new Exception("thread is already running");

            // if thread has previously been aborted we need to construct a new thread, so just build one anyway
            _workerThread = new Thread(new ParameterizedThreadStart(TrainWorker));

            _workerThread.Start(TrainingSet);
        }

        private void TrainWorker(Object args)
        {
            TrainingSet set = args as TrainingSet;
            base.Train(set);
        }

        [CategoryAttribute("Status"), Browsable(true), ReadOnly(true), BindableAttribute(false), DesignOnly(false), DescriptionAttribute("Is the LCS running?")]
        public bool IsAlive
        {
            get
            {
                return _workerThread.IsAlive;
            }
        }

        public void Abort()
        {
            _workerThread.Abort();
        }
    }
}
